<template>
	<view class="page_play">

		<view class="availabilityList">
			<view class="shopList">
				<u--image :src="itemshop.mediaUrl || '../../../static/default/default.png'" width="100px" height="100px"
					radius="5"></u--image>
				<view class="description">
					<view class="title">
						<view class="description_title">
							{{itemshop.communityName}}
						</view>
						<view class="titleType error" v-if="itemshop.payState == 0">
							未支付
						</view>
						<view class="titleType success" v-if="itemshop.payState == 1">
							已支付
						</view>
					</view>
					<view class="description_squaremetre">
						{{ itemshop.houseRoomName}}
					</view>
					<view class="row">
						<view class="money">
							{{itemshop.serviceCharge}}
							<view class="unit">
								元/月
							</view>
						</view>
						<!-- 	<view class="buttonRow" v-if="itemshop.payState == 0">
							<u-button type="primary" size="small" text="去支付"
								@click="confirmLease(itemshop)"></u-button>
						</view> -->
					</view>
				</view>
			</view>

		</view>

		<view style="width: 100%; text-align: right;margin: 50rpx 0;">
			需支付：<text style="color: red; font-weight: bold; ">{{itemshop.serviceCharge}}元</text>
		</view>
		<u-radio-group placement="row" v-model="playState">
			<u-cell-group>
				<u-cell>
					<view slot="title">
						<u-icon name="weixin-fill" color="#03c402" size="28" label="微信支付"></u-icon>
					</view>
					<view slot="right-icon">

						<u-radio activeColor="#1296db" :name="1" @change="changePlay($event)"></u-radio>

					</view>
				</u-cell>
				<u-cell>
					<template slot="title">
						<u-icon name="zhifubao" color="#1296db" size="28" label="支付宝支付"></u-icon>
					</template>
					<template slot="right-icon">
						<u-radio activeColor="#1296db" :name="0" @change="changePlay($event)"></u-radio>
					</template>
				</u-cell>

			</u-cell-group>
		</u-radio-group>

		<view style="position: absolute; bottom: 120rpx; width: 90%;left: 50%; transform: translateX(-50%);">
			<u-button type="primary" text="去支付" round="20" @click="confirmLease(itemshop)"></u-button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		serviceOrderPay,
	} from "@/api/home/index.js"
	export default {
		data() {
			return {
				playState: 0,
				itemshop: {},
			}
		},
		onLoad(option) {
			console.log(JSON.parse(option.itemshop), 12321332);
			this.itemshop = JSON.parse(option.itemshop)

		},
		methods: {
			changePlay(state) {
				this.playState = state;
			},
			//去支付
			async confirmLease(itemshop) {
				let data = {
					payType: this.playState,
					outTradeNo: itemshop.orderNo, //订单号
					totalPrice: itemshop.serviceCharge, //支付金额
					description: "信息服务费" //订单描述
				}

				const res = await serviceOrderPay(data)
				if (res.data.code != 200) return this.$refs.uToast.show({
					message: res.data.msg,
					type: 'error'
				})

				// var EnvUtils = plus.android.importClass('com.alipay.sdk.app.EnvUtils');
				// EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
			 	
				var that = this
				uni.requestPayment({
					"provider": that.playState == 1 ? "wxpay" : 'alipay',
					"orderInfo": that.playState == 1 ? {
						"appid": res.data.data.appid, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
						"noncestr": res.data.data.nonceStr, // 随机字符串
						"package": res.data.data.packageVal, // 固定值
						"partnerid": res.data.data.partnerId, // 微信支付商户号
						"prepayid": res.data.data.prepayId, // 统一下单订单号 
						"timestamp": res.data.data.timestamp, // 时间戳（单位：秒）
						"sign": res.data.data.sign // 签名，这里用的 MD5/RSA 签名
					} : res.data.msg,
					success(res) {
						uni.showToast({
							title: '支付成功',
							icon: 'success'
						})
						uni.redirectTo({
							url: './palySuccess?itemshop=' + JSON.stringify(that.itemshop) + '&iconType=' + that.playState,
						})
					},
					fail(e) {
						console.log(e)
					 
						uni.showToast({
							title: '用户取消支付',
							icon: 'error'
						})
					}
				})

			},

		},


	}
</script>

<style lang="scss" scoped>
	@import "~@/styles/index.scss";
	@import "~@/styles/global.scss";

	.broker {
		display: inline-block;
		float: right;
		margin-top: 30rpx;
	}

	::v-deep .u-radio-label--right[data-v-5dfbf8b1] {
		flex-direction: row-reverse;
		justify-content: space-between;
		width: 100%;
	}

	.page_play {
		padding: 10rpx 10rpx 0 10rpx;
		box-sizing: border-box;
		height: 100vh;
		background-color: $pagebgc;

		.availabilityList {
			width: 100%;
			box-sizing: border-box;
			padding: 20rpx;
			@include boxshadow;
			margin-top: 30rpx;
			border-radius: 20rpx;
			background-color: #fff;

			.tiem {
				// text-align: center;
				color: #7f7f7f;
				font-size: 24rpx;
				padding: 20rpx 0;
			}

			.shopList {
				border-radius: 10rpx;
				display: flex;
				// padding: 20rpx;
				margin-bottom: 20rpx;
				background-color: white;

				.description {
					width: 100%;
					margin-left: 20rpx;

					.title {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.description_title {
							font-weight: bold;
							font-size: 36rpx;
						}

						.titleType {
							font-weight: bold;
							font-size: 32rpx;
						}

						.success {
							color: #7f7f7f;
						}

						.error {
							color: #d02d37;
						}
					}

					.description_squaremetre {
						color: #7f7f7f;
						font-size: 28rpx;
						padding: 10rpx 0;
					}
				}

				.buttonRow {
					display: flex;
					justify-content: flex-end;
				}

				.buttonRow_ {
					width: 30%;
					margin-left: auto;
				}

				.button_row {
					display: flex;
					justify-content: flex-start;
					flex-wrap: wrap;
					overflow: hidden;
					margin-top: 20rpx;
				}

				.row {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin: 5rpx 0;

					.money {
						font-weight: bold;
						font-size: 42rpx;
						color: red;
						display: flex;
						align-items: center;

						.unit {
							font-size: 28rpx;
						}
					}

					.buttom {
						display: flex;
						align-items: center;

						.custom-style {
							margin-left: 10rpx;
						}
					}

				}

			}
		}
	}
</style>